package com.xsm.activitygroup;

import java.util.ArrayList;

import com.xsm.adapter.List1Adapter;
import com.xsm.object.CTextObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FragmentA  extends Fragment{
	private Classification context;
	private ArrayList<CTextObject> arrayListA;
	private List1Adapter adapter;
	private boolean select[];
	private CTextObject selecTextObject;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, container,false);
	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context=(Classification)getActivity();
		arrayListA=(ArrayList<CTextObject>)context.getIntent().getExtras().get("arrayListA");
		selecTextObject=arrayListA.get(0);
		final FragmentB fragmentb=(FragmentB)getFragmentManager().findFragmentById(R.id.fragment2);
		ListView listView=(ListView)context.findViewById(R.id.list1);
		ArrayList<String> valuesA=new ArrayList<String>();
		for(int i=0;i<arrayListA.size();i++){
			valuesA.add(arrayListA.get(i).getName());
		}
		     adapter=new List1Adapter(valuesA, context);
		     adapter.SetitemPressbg(R.drawable.list1selectbg);
		     adapter.Setitembg(R.drawable.list1bg);
		     adapter.initselect();
		     adapter.setSelect(0, true);
		     select=adapter.getselect();
		     listView.setAdapter(adapter);
		     
		     listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if(arrayListA.get(position).getTextid().equals("-1")){
				   		Intent intent =new Intent();
			      		intent.putExtra("textid",arrayListA.get(position).getTextid());
			      		intent.putExtra("textString",arrayListA.get(position).getName());
			      		getActivity().setResult(getActivity().RESULT_OK,intent);
						getActivity().finish();
					return ;	
					}
					  for(int i=0;i<select.length;i++){
						if(position==i){
							adapter.setSelect(i, true);		
						}
						else
						adapter.setSelect(i, false);	
						selecTextObject=arrayListA.get(position);
						adapter.notifyDataSetChanged();
						fragmentb.UpdateSelectList(selecTextObject);
					}
				}
			});
	}
	
	public CTextObject getObject(){
		return selecTextObject;
	}
}
